package ui.units {
	import beans.IBean;
	import beans.MonsterBean;
	
	import flash.display.MovieClip;
	
	import globals.Global;
	
	import ui.units.buffs.BuffData;
	
	import util.GeomUtil;
	
	public class Monster extends BaseUnit{
		
		public var speed:int;
		public var endX:Number;
		public var endY:Number;
		
		//monster move path
		public var path:Array;
		//whichr point or position of the path the monster is  
		public var node:uint;
		private var totalNode:uint;
		
		private var bloodBar:BloodBar;
		public var state:uint;
//		private var hitEffect:MovieClip;

		public function Monster() {
			super();
			path = new Array();
			endX = endY = 0;
			
			bloodBar = new BloodBar();
			bloodBar.x = -20;
			bloodBar.y = -66;
			addChild(bloodBar);
			
			path = Global.mapManager.getPath();
			totalNode = path.length;
			var point:Array = path.shift();
			endX = point[0] * 39 + 20;
			endY = point[1] * 39 + 20;
		}
		
		override public function init(bean:UnitBean):void {
			super.init(bean);
			view.scaleX = view.scaleY = 0.3;
			bloodBar.max = bean.life;
			state = UnitState.LIVE;
		}
		
		public function move():void {
			if (state == UnitState.DEAD) {
				return;
			}
			if (GeomUtil.distance2(x, y, endX, endY) < speed * speed) { 
//			if (x == endX && y == endY) {
				if (path.length <= 0) {
					Global.battleManager.monsterPass(bean.id);
					Global.unitManager.removeMonster(bean.id);
					return;
				}
				var point:Array = path.shift();
				endX = point[0] * 39 + 20;
				endY = point[1] * 39 + 20;
			}
			if (endX > x) {
				direction = UnitBean.RIGHT;
				x += speed;
			}
			if (endX < x) {
				direction = UnitBean.LEFT;
				x -= speed;
			}
			if (endY > y) {
				direction = UnitBean.DOWN;
				y += speed;
			}
			if (endY < y) {
				direction = UnitBean.UP;
				y -= speed;
			}
		}
		
		public function hit():void {
			
		}
		
		public function addBuff(buff:BuffData):void {
			
		}
		
		override public function update():void {
			move();
			super.update();
		}
		
		public function hitBlood(value:Number):void {
			if (Global.unitManager.getMonster(bean.id) == null || state == UnitState.DEAD) {
				return;
			}
			bean.life -= value;
//			var hitEffect:MovieClip = Global.assetsManager.getAssetsAsMovieClip("Effect1", "tower");
//			hitEffect.x = -hitEffect.width * 0.5;
//			hitEffect.y = -hitEffect.height * 0.5;
//			hitEffect.addFrameScript(7, function():void {
//				hitEffect.stop();
//				removeChild(hitEffect);
//				hitEffect = null;
//			});
//			addChild(hitEffect);
			bloodBar.reduce(value);
			if (bean.life <= 0) {
				die();
			} else {
//				bloodBar.reduce(value);
			}
		}
		
		private function die():void {
			state = UnitState.DEAD;
			Global.game.unitLayer.setChildIndex(this, 0);
			Global.battleManager.monsterDie(bean.id);
			view.mc.addFrameScript(view.mc.totalFrames - 1, function():void {
				Global.unitManager.removeMonster(bean.id);
			});
			view.gotoAndPlay("dead");
//			Global.unitManager.removeMonster(bean.id);
		}
	}
}


import flash.display.Bitmap;
import flash.display.Shape;
import flash.display.Sprite;

import globals.Global;

class BloodBar extends Sprite {
	
	private var _max:Number;
	private var current:Number;
	private var bar:Bitmap;
	private var _mask:Shape;
	
	public function BloodBar():void {
	}
	
	public function set max(value:Number):void {
		_max = value;
		
		var bg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("blood_bar_bg", "ui"));
		addChild(bg);
		
		bar = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("blood_bar_track", "ui"));
		bar.x = 2;
		bar.y = 2;
		addChild(bar);
		
		_mask = new Shape();
		_mask.x = 2;
		_mask.y = 2;
		bar.mask = _mask;
		addChild(_mask);
		
		current = _max;
		updateBar();	
	}
	
	private function updateBar():void {
		_mask.graphics.clear();
		_mask.graphics.beginFill(0xFF0000);
		_mask.graphics.drawRect(1, 1, uint(current / _max * 39), 4);
		_mask.graphics.endFill();
	}
	
	public function update(value:Number):void {
		current = value;
		updateBar();
	}
	
	public function reduce(value:Number):void {
		current = current > value ? (current - value) : 0;
		updateBar();
	}
}